/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef WOWPTR_H
#define WOWPTR_H

#include <SimpleList.h>

namespace liba
{

class WowPtrBase : public SimpleListNode<WowPtrBase>
{
public:
    virtual void object_deleted()=0;
};

template< typename T >
class WowPtr : public WowPtrBase
{
public:
///	WowPtr()
///	:	pointer( 0 )
///	{}
	WowPtr( T * pointer )
	:	pointer( pointer )
	{
		if( pointer )
			pointer->ptr_added( this );
	}

	WowPtr( const WowPtr & wow )
	{
		pointer = wow.pointer;
		if( pointer )
			pointer->ptr_added( this );
	}

	const WowPtr & operator=( const WowPtr & wow )
	{
		if ( this != &wow )
		{
			if( pointer )
				unlink();
			pointer = wow.pointer;
			if( pointer )
				pointer->ptr_added( this );
		}
		return *this;
	}

	~WowPtr()
	{
		if( pointer )
			unlink();
	}

	virtual void object_deleted()
	{
		pointer = 0;
		unlink();
	}

	const T * operator->()const
	{
		return pointer;
	}
	T * operator->()
	{
		return pointer;
	}
	const T & operator*()const
	{
		return *pointer;
	}
	T & operator*()
	{
		return *pointer;
	}
/*	operator const T *()const
	{
		return pointer;
	}*/
	operator T *()const
	{
		return pointer;
	}
private:    
	T * pointer;
};

class WowPointable
{
public:
	virtual ~WowPointable();
	void ptr_added( WowPtrBase * ptr );
private:
	SimpleListNode<WowPtrBase> wow_list_start;
};


} // namespace liba
using namespace liba;

#endif //WOWPTR_H